/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.transitions.TweenMS;

import fever.display.effects.TweenEngine;

import fvaswing.effects.FvTweenEffect;

/**
 * Move effect on <strong>AsWing components</strong>
 * 
 * <p>Effect changes the position of a target over a specified time 
 * interval.<br />
 * You can specify the initial position with the xFrom and yFrom values, 
 * the destination position with xTo  and yTo, or the number of pixels to 
 * move the component with xBy and yBy.
 * 
 * <p>If you specify any two of the values (initial position, destination, 
 * or amount to move), {@link FvMove} calculates the third.<br />
 * If you specify all three, Fever ignores the xBy and yBy values.<br /> 
 * If you specify only the xTo and yTo values or the xBy and yBy values, 
 * {@link FvMove} sets xFrom and yFrom to be the object's current position.
 * 
 * <p>Example
 * {@code
 *   public function test() : Void
 *   {
 *     var button : JButton = new JButton( "move button" );
 *     
 *     var effect : FvMove = new FvMove( button );
 *     effect.addEventListener( FvEffectEvent.onEffectEndEVENT, this, _onEnd );
 *     effect.xTo = 200;
 *     effect.yTo = 100;
 *     effect.startDelay = 2000;
 *     effect.easing = Bounce.easeOut;
 *     
 *     effect.play();
 *   }
 * }
 * 
 * @author Romain Ecarnot
 */
class fvaswing.effects.FvMove extends FvTweenEffect
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _endX : Number;
	private var _endY : Number;
	private var _startX : Number;
	private var _startY : Number;
	
	private var _storeX : Number;
	private var _storeY : Number;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Number of pixels to move the components along the x axis. */
	public var xBy : Number;
	
	/** Initial position's x coordinate. */
	public var xFrom : Number;
	
	/** Destination position's x coordinate. */
	public var xTo : Number;
	
	/** Number of pixels to move the components along the y axis. */
	public var yBy : Number;
	
	/** Initial position's y coordinate. */
	public var yFrom : Number;
	
	/** Destination position's y coordinate. */
	public var yTo : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param t AsVing component target
	 */
	public function FvMove( t )
	{
		super( t );
		
		xBy = Number.NaN;
		xFrom = Number.NaN;
		xTo = Number.NaN;
		yBy = Number.NaN;
		yFrom = Number.NaN;
		yTo = Number.NaN;
	}
	
	/**
	 * Starts effect.
	 * 
	 * <p>Stores current position for loop process.
	 */
	public function play() : Void
	{
		if( !_isPlaying )
		{
			_storeX = _instance.x;
			_storeY = _instance.y;
			
			super.play();
		}	
	}
	
	/**
	 * Ends effects.
	 * 
	 * <p>Overloads {@link FvTweenEffect#end} method.
	 */
	public function end() : Void
	{
		_tween.stop();
		
		_instance.x = _endX;		_instance.y = _endY;

		_endEffect();
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Builds tweens instance
	 */
	private function _buildTween() : Void
	{
		_startX = ( isNaN( xFrom ) ) ?  ( _isRepeated ) ? _storeX : _instance.x : xFrom;
		_startY = ( isNaN( yFrom ) ) ?  ( _isRepeated ) ? _storeY : _instance.y : yFrom;
		
		_endX = ( isNaN( xBy ) ) ?  ( isNaN( xTo ) ) ?  _instance.x : xTo : xBy;
		_endY = ( isNaN( yBy ) ) ?  ( isNaN( yTo ) ) ?  _instance.y : yTo : yBy;
		
		_tween = TweenEngine.getInstance().getMultiTween( _instance, [ 'x', 'y' ], [ _endX, _endY ], duration, [ _startX, _startY ], easing );
		_tween.addEventListener( TweenMS.onMotionChangedEVENT, this );
		_tween.addEventListener( TweenMS.onMotionFinishedEVENT, this );
	}
}